package com.nuoniu.sibanyun.controller.finance;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.AccountingVoucherDto;
import com.nuoniu.sibanyun.entity.finance.VAccountingVoucherMain;
import com.nuoniu.sibanyun.service.finance.IVAccountingVoucherMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会计凭证主表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-09
 */
@RestController
@RequestMapping("/finance/accountingVoucher")
@Api(tags = "会计凭证管理")
public class VAccountingVoucherMainController extends BaseController {

    @Autowired
    private IVAccountingVoucherMainService accountingVoucherMainService;

    @ApiOperation("获取编码")
    @GetMapping("getCode")
    public HttpResult getCode(String businessDate) {
        return HttpResult.ok(accountingVoucherMainService.getCode(LocalDate.parse(businessDate)));
    }

    @ApiOperation("分页获取列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, AccountingVoucherDto accountingVoucher) {
        return HttpResult.ok(accountingVoucherMainService.pageList(page, accountingVoucher));
    }

    @GetMapping("/getOneByCondition")
    @ApiOperation(value = "通过ID获取某个会计凭证的数据")
    public HttpResult getOneByCondition(Integer actionType,Long currentId) {
        return HttpResult.ok(accountingVoucherMainService.getOneByCondition(actionType, currentId));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VAccountingVoucherMain accountingVoucherMain) {
        if(StringUtils.isBlank(accountingVoucherMain.getCode())) {
            throw new NuoNiuException("编号不能为空");
        }
        if(accountingVoucherMain.getVClassId() == null) {
            throw new NuoNiuException("凭证类别不能为空");
        }
        if(accountingVoucherMain.getAccountingVoucherDetails().size() < 2) {
            throw new NuoNiuException("明细至少两条");
        }
        if(accountingVoucherMain.getBusinessDate() == null) {
            throw new NuoNiuException("业务日期不能为空");
        }
        if(accountingVoucherMain.getBusinessDate().isAfter(LocalDate.now())) {
            throw new NuoNiuException("业务日期不能超过今天");
        }
//        if(accountingVoucherMain.getAccountingVoucherSummaries().size() == 0) {
//            throw new NuoNiuException("汇总不能为空");
//        }

        Long id = accountingVoucherMainService.saveAccountingVoucher(accountingVoucherMain);
        return HttpResult.ok(id);
    }

    @PostMapping("examine")
    @ApiOperation("单据审核/弃审")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "单/多个单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(@RequestBody Map<String, Object> data) {
        List<Long> ids = (List<Long>)data.get("ids");
        Integer status = (Integer) data.get("status");
        return HttpResult.ok(accountingVoucherMainService.examine(ids, status));
    }

    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id, Integer status) {
        accountingVoucherMainService.deleteById(id, status);
        return HttpResult.ok();
    }

    @ApiOperation("记账")
    @PostMapping("book")
    public HttpResult book(@RequestBody List<Long> ids) {
        return HttpResult.ok(accountingVoucherMainService.book(ids));
    }
    @ApiOperation("取消记账")
    @PostMapping("unBook")
    public HttpResult unBook(@RequestBody List<Long> ids) {
        return HttpResult.ok(accountingVoucherMainService.unBook(ids));
    }
    @ApiOperation("整理凭证")
    @GetMapping("collateAccounting")
    public HttpResult collateAccounting(Integer accountPeriod) {
        accountingVoucherMainService.collateAccounting(accountPeriod);
        return HttpResult.ok();
    }
}
